package com.nondo.dean.platform.controller.wap.app;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Customer;
import com.nondo.dean.platform.model.ActivityCustomer;
import com.nondo.dean.platform.service.CustomerService;
import com.nondo.dean.platform.service.UserService;

@Controller("apiWapCustomer")
@RequestMapping("/wap/app/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;
    
    @Autowired
    UserService userService;

    /**
     * 采集用户信息
     * 
     * @param name
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping("/collect")
    public View<Integer> updateCustomer(HttpServletRequest request, HttpServletResponse response,
        @RequestBody ActivityCustomer activityCustomer) {
        View<Integer> view = new View<Integer>(20000, "更新成功");
        Customer customer = ActivityCustomer.copyFrom(activityCustomer);
        Integer userId = customer.getUserId();
        if (userId !=null && userId != 0) {
            customer.setStoreId(userService.queryStoreId(userId));
        }
        long count = customerService.countByActivityIdAndPhone(customer);
        if (count > 0) {
            return View.ofError("该手机号已经参加过此活动");
        }
        customerService.addCustomer(customer);
        view.setData(customer.getStoreId());
        return view;
    }
}
